package com.block.datapush.service.apipush;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.model.param.PushApiKeyParam;
import com.block.common.service.IMemberCommonService;
import com.block.common.service.LogFactory;
import com.block.datapush.enums.PushStatusEnum;
import com.block.datapush.model.PushQueryParam;
import com.block.datapush.model.dto.NoticeParamDTO;
import com.block.datapush.model.result.ProtocolParamResult;
import com.block.datapush.model.result.PushApiResult;
import com.block.datapush.service.IPushApiService;
import com.block.datapush.service.apipush.utils.ZhengPuUtil;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 *  成都正谱
 */
@Service
public class ZhengPuApiService implements IPushApiService {

    private static final Logger log = LogFactory.getPushLog();

    @Autowired
    IMemberCommonService memberCommonService;


    @Override
    public PushApiResult checkUser(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {

        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);

        String mobileMd5 = memberSourceInfo.getMobileMd5().toLowerCase();
        String queryUrl = pushApiKeyParam.getCheckUrl();
        String secret = pushApiKeyParam.getApiSecret();
        String apiCode = pushApiKeyParam.getApiCode();

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("md5",mobileMd5);

        String tranData = SecureUtil.aes(secret.getBytes()).encryptBase64(JSON.toJSONString(stringObjectHashMap));

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", apiCode);
        paramMap.put("data", tranData);

        log.info("【成都正谱】撞库开始【queryUrl】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(queryUrl)
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("【成都正谱】撞库结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer code = resultObject.getInteger("code");
        String message = resultObject.getString("message");
        String traceid = resultObject.getString("traceid");


        pushApiResult.setCode(code + "" );
        pushApiResult.setMessage(traceid+"|"+message);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "200") && StringUtils.isNotEmpty(traceid)) {
            pushApiResult.setPushSuccess(true);
        }
        return pushApiResult;
    }

    @Override
    public PushApiResult queryApprove(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {
        return null;
    }

    @Override
    public PushApiResult registerUser(String orderNo, String outOrderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        pushApiResult.setOrderNo(orderNo);

        String requestUrl = pushApiKeyParam.getRegisterUrl();
        String secret = pushApiKeyParam.getApiSecret();
        String apiCode = pushApiKeyParam.getApiCode();


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("name",memberSourceInfo.getRealname());
        paramMap.put("age",memberSourceInfo.getAge());
        paramMap.put("gender", ZhengPuUtil.getSex(memberSourceInfo.getSex()));
        paramMap.put("telephone",memberSourceInfo.getMobile());
        paramMap.put("area",memberSourceInfo.getCity());
        paramMap.put("loanAmount",ZhengPuUtil.getloan(memberSourceInfo.getLoanAmount()));
        paramMap.put("loanTime",ZhengPuUtil.getLoanTerm(memberSourceInfo.getLoanTime()));
        paramMap.put("hasJob",ZhengPuUtil.getJob(memberSourceInfo.getGongzuo()));
        paramMap.put("hasHouse",ZhengPuUtil.getHouse(memberSourceInfo.getHouse()));
        paramMap.put("hasCar",ZhengPuUtil.getCar(memberSourceInfo.getCar()));
        paramMap.put("hasInsurance", ZhengPuUtil.getBaoxian(memberSourceInfo.getBaoxian()));
        paramMap.put("hasSocialInsurance", ZhengPuUtil.getshebao(memberSourceInfo.getShebao()));
        paramMap.put("hasIncome", 1);
        paramMap.put("hasFund", ZhengPuUtil.getGjj(memberSourceInfo.getGjj()));
        paramMap.put("hasOverdue", ZhengPuUtil.getYuqi(memberSourceInfo.getYuqi()));
        paramMap.put("hasCreditCard", ZhengPuUtil.getXinyongka(memberSourceInfo.getXinyongka()));
        paramMap.put("hasEnterprise",ZhengPuUtil.getJobMark(memberSourceInfo.getGongzuo()));
        paramMap.put("aliScore", ZhengPuUtil.getZhima(memberSourceInfo.getZhima()));
        paramMap.put("WXScore", ZhengPuUtil.getWeili(memberSourceInfo.getWeili()));


        String tranData = SecureUtil.aes(secret.getBytes()).encryptBase64(JSON.toJSONString(paramMap));

        Map<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("appid", apiCode);
        stringObjectHashMap.put("data", tranData);

        log.info("【成都正谱】导单开始【queryUrl】{}【paramMap】{}", requestUrl, JSON.toJSONString(stringObjectHashMap));
        String result = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(stringObjectHashMap))
                .execute().body();
        log.info("【成都正谱】导单结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer code = resultObject.getInteger("code");
        String message = resultObject.getString("message");
        String traceid = resultObject.getString("traceid");

        pushApiResult.setCode(code + "|" );
        pushApiResult.setMessage(traceid+"|"+message);
        pushApiResult.setParamInfo(JSON.toJSONString(stringObjectHashMap));
        pushApiResult.setResultInfo(result);


        if (StrUtil.equals(String.valueOf(code), "200")) {
            pushApiResult.setPushSuccess(true);
            pushApiResult.setSettleFlag(true);
            pushApiResult.setStatus(PushStatusEnum.SUCCESS.getValue());
            return pushApiResult;
        }
        pushApiResult.setPushSuccess(false);
        pushApiResult.setSettleFlag(false);
        pushApiResult.setStatus(PushStatusEnum.FAILED.getValue());
        return pushApiResult;
    }

    @Override
    public PushApiResult queryResult(PushQueryParam pushQueryParam, PushApiKeyParam pushApiKeyParam, String extJson) {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        return pushApiResult;
    }



    @Override
    public NoticeParamDTO getNoticeResultParam(String infoId, Map<String, String[]> parameterMap, String paramContent) {
        NoticeParamDTO noticeParamDTO = new NoticeParamDTO();
        return noticeParamDTO;
    }

    @Override
    public JSONObject getNoticeResultReturn(Map<String, String[]> parameterMap, String paramContent, JSONObject extObject) {
        JSONObject jsonObject = new JSONObject();
        return jsonObject;
    }

    @Override
    public ProtocolParamResult getProtocolResult(MerchantBaseInfo merchantBaseInfo, MemberSourceInfo memberSourceInfo) {
        ProtocolParamResult paramResult = new ProtocolParamResult();
        return paramResult;
    }

}
